<script src="./login.component.js"></script>
<style src="./login.css" scoped></style>
<template>
	<view class="content padding-xl">
		<view class="text-center padding-tb-xl">
			<image :src="'/static/logos.png'" class='logo'></image>
		</view>
		<view class="btn-row margin-top-xl">
			<!-- #ifdef MP-WEIXIN -->
			<button type="primary" class="primary bg-red" open-type="getPhoneNumber"  @getphonenumber="getPhoneNumber" :disabled="checkbox=='1'">微信授权登录</button>
			<!-- #endif -->
			<button type="primary" class="primary bg-f5f5f5 margin-top" @tap="phoneLogin" :disabled="checkbox=='1'">手机号码登录</button>
		</view>
		<view class="padding-top flex align-center text-df">
			<checkbox-group class="inline-block" @change="CheckboxChange">
					<label class="flex align-center"><checkbox  class='red round' :checked="checkbox=='0'?true:false" value="0"  style="transform: scale(0.6)"></checkbox>登录即代表同意我们</label>
			</checkbox-group>
			<navigator class="text-red inline-block" url="../xieyi/fwxieyi">《服务协议》</navigator>
			<navigator class="text-red inline-block" url="../xieyi/ysxieyi">《隐私协议》</navigator>
		</view>
	</view>
</template>
<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	export default {
		components: {
		},
		data() {
			return {
				info:'',
				disabled:true,
				checkbox: '0',
			}
		},
		computed:{
			...mapState(['hasLogin']),
		},
		methods: {
			CheckboxChange(e) {
				this.checkbox = e.detail.value
				
				if(this.checkbox==""){
					//alert(1);
					this.checkbox = 1
				}else{
					//alert(2);
					this.checkbox = 0
				}
			},
			phoneLogin() {
				uni.navigateTo({
					url: '../codelogin/codelogin',
				});
			},
			//获取用户信息
			// getUserInfo() {
			//     let that =this
			// 	uni.getUserInfo({
			// 		provider: 'weixin',
			// 		success: (res) => {
			// 			that.info = res.userInfo;
			// 			//console.log(res)
			// 			console.log(res.userInfo)
			// 			uni.setStorageSync('avatarUrl',that.info.avatarUrl);
			// 			uni.setStorageSync('nickName',that.info.nickName)
			// 		},
			// 		fail: (err) => {
			// 			console.log(err)
			// 			uni.showToast({
			// 				icon:"none",
			// 				title:'获取用户信息失败'
			// 			})
			// 		}  
			// 	})
			// },
			getPhoneNumber(e) {
				var that = this;
				if (e.detail.errMsg == 'getPhoneNumber:fail:user deny' || e.detail.errMsg == 'getPhoneNumber:fail user deny') {
					uni.showToast({title:"用户拒绝提供手机号",icon:"none"});
				}else{
					let iv = e.detail.iv;
					let encryptedData = e.detail.encryptedData;
					//let authId = res1.data.data.authId;
					uni.request({
						url: `${that.$serverUrl}/isalary/login/byWeChatApplet`,
						method:'POST',
						data: {
							iv: iv,
							encryptedData: encryptedData,
							authId: uni.getStorageSync('authId')
						},
						header: {
							'content-type': 'application/json;charset=utf-8', 
						},
						success: (res) => {
							//console.log(res.data.data.token);
							if (res.data.code == 0) {
								that.login(res.data.data);
								that.setToken(res.data.data);
								
								that.profileComfirm();
								//that.auditSubscriptionsSetting();
								// setTimeout(()=>{
								// 	uni.reLaunch({
								// 		url: '../home/home',
								// 	});
								// }, 200)
								//that.getUserInfo();
								
							} else {
								uni.showToast({
									icon: 'none',
									title: res.data.msg
								});
							}
						},
						fail:function(err){
							uni.showToast({
								icon: 'none',
								title: err
							});
						}
					})
				}
			},
			// 获取授权信息
			profileComfirm(){
				uni.showModal({
				  title: '获取信息',
				  content: '获取你的昵称、头像、地区及性别',
				  success: res => {
				    if (res.confirm) {
						if(uni.getUserProfile){
							uni.getUserProfile({
								desc:'获取信息',
								success:(res)=>{
									uni.setStorageSync('nickName',res.userInfo.nickName)
									uni.setStorageSync('avator',res.userInfo.avatarUrl);
									this.auditSubscriptionsSetting();
								},
								fail:(res)=>{
									console.log(res);
									this.auditSubscriptionsSetting();
								}
							});
						}else{
							uni.showToast({
								icon: 'none',
								title: "请更新最新微信客户端"
							});
							this.auditSubscriptionsSetting();
						}
					}else if(res.cancel){
						this.auditSubscriptionsSetting();
					}
				  }
				})
			},
			// 判断是否授权微信订阅消息
			auditSubscriptionsSetting() {
			      let that = this
			      const tmplId = '1mCqh4nH56ivmrwVwqOGVKqB7wlNHVmkJwduikjEWtU'
			      uni.getSetting({
			        withSubscriptions: true,
			        success(res) {
			          console.log(res)
			          if (res.subscriptionsSetting && res.subscriptionsSetting.mainSwitch) {
			            if (res.subscriptionsSetting.itemSettings && res.subscriptionsSetting.itemSettings[tmplId]) {
			              let item = res.subscriptionsSetting.itemSettings[tmplId]
			              if (item == 'reject') {
			                console.log('提示：用户拒绝订阅消息')
			                that.dingyueComfirm(tmplId)
			              } else if (item == 'accept') {
			                console.log('提示：您已经开启订阅消息')
							setTimeout(()=>{
								uni.reLaunch({
									url: '../home/home',
								});
							}, 200)
			              } else if (item == 'ban') {
			                console.log('提示：您已经被后台封禁')
							setTimeout(()=>{
								uni.reLaunch({
									url: '../home/home',
								});
							}, 200)
			              }
			            } else {
			              console.log('提示：用户没有勾选订阅消息或者没有状态')
			              that.dingyueComfirm(tmplId)
			            }
			          } else {
			            console.log('提示：订阅消息主开关没打开')
			            that.dingyueComfirm(tmplId)
			          }
			        }
			      })
			 },
			// 订阅信息
			dingyueComfirm: function(tmplId) {
			      uni.showModal({
			        title: '订阅消息',
			        content: '请同意我们给您推送信息，以便接受到账提醒。',
			        success: res => {
			          if (res.confirm) {
			            uni.requestSubscribeMessage({
			              tmplIds: [tmplId],
			              success: res => {
			                if (res[tmplId] === 'accept') {
			                  uni.showToast({
			                    title: '订阅成功！',
			                    icon: 'success'
			                  })
			                } else if (res[tmplId] == 'reject') {
			                  //引导用户，手动引导用户点击按钮，去设置页开启，## Modals是自定义组件
			                  uni.showModal({
			                    title: '订阅消息',
			                    content: '您当前拒绝接受消息通知，是否去开启',
			                    confirmText: '开启授权',
			                    confirmColor: '#345391',
			                    cancelText: '仍然拒绝',
			                    cancelColor: '#999999',
			                    success(res) {
			                      if (res.confirm) {
			                        console.log('用户点击确定')
			                        uni.openSetting({
			                          success(res) {
			                            console.log(res.authSetting)
			                          },
			                          fail(err) {
			                            //失败
			                            console.log(err)
			                          }
			                        })
			                      } else if (res.cancel) {
			                        console.log('用户点击取消')
			                      }
			                    }
			                  })
			                }
							setTimeout(()=>{
								uni.reLaunch({
									url: '../home/home',
								});
							}, 200)
			              },
			              fail(err) {
			                //失败
			                console.log(err)
			              }
			            })
			          }else if(res.cancel){
						  setTimeout(()=>{
						  	uni.reLaunch({
						  		url: '../home/home',
						  	});
						  }, 200)
					  }
			        }
			      })
			 },
			...mapMutations(['login','setToken']),
		}
	}
</script>

<style>
	page{
		background-color: #FFFFFF;
	}
	/* .action-row {
		display: flex;
		flex-direction: row;
		justify-content: center;
	}

	.action-row navigator {
		color: #007aff;
		padding: 0 10px;
	} */
	.input-row{
		padding: 15px 0;
	}
	.logo{
		width: 206upx;
		height: 244upx;
	}
</style>